#include "recorddetail.h"
#include "dataprovider.h"
#include "utils.h"
#include <QVBoxLayout>
#include "debouncehandler.h"

QHBoxLayout* RecordDetail::createLabelLine(QString title, QLabel* content){
    QHBoxLayout* lineLayout = new QHBoxLayout;
    auto label = new QLabel(title + ":", this);
    label->setAlignment(Qt::AlignRight);
    label->setFixedWidth(44);
    label->setProperty("IsTitle", true);
    lineLayout->addWidget(label);
    lineLayout->addWidget(content);
    return lineLayout;
}

RecordDetail::RecordDetail(QWidget *parent)
    : QWidget{parent}
{
    setFixedWidth(240);
    record = nullptr;

    nameLabel = new QLabel(this);
    nameLabel->setAlignment(Qt::AlignHCenter);
    shortNameLabel = new QLabel(this);
    makeAtLabel = new QLabel(this);
    vailAtLabel = new QLabel(this);
    remarkLabel = new QLabel(this);
    saveTypeLabel = new QLabel(this);
    standradLabel = new QLabel(this);
    hasPartLabel = new QLabel(this);
    statusLabel = new QLabel(this);
    reprintButton = new QPushButton("重打印", this);
    splitButton = new QPushButton("分装", this);
    expiredButton = new QPushButton("作废", this);
    nameLabel->setText("未知");

    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addWidget(nameLabel);

    auto lineLayout = createLabelLine("简拼", shortNameLabel);
    mainLayout->addLayout(lineLayout);
    lineLayout = createLabelLine("状态", statusLabel);
    mainLayout->addLayout(lineLayout);
    lineLayout = createLabelLine("制作", makeAtLabel);
    mainLayout->addLayout(lineLayout);
    lineLayout = createLabelLine("赏味期", vailAtLabel);
    mainLayout->addLayout(lineLayout);
    lineLayout = createLabelLine("保存", saveTypeLabel);
    mainLayout->addLayout(lineLayout);
    lineLayout = createLabelLine("标准", standradLabel);
    mainLayout->addLayout(lineLayout);
    lineLayout = createLabelLine("可拆分", hasPartLabel);
    mainLayout->addLayout(lineLayout);

    auto label = new QLabel("备注:", this);
    label->setProperty("IsTitle", true);
    label->setFixedWidth(44);
    label->setAlignment(Qt::AlignRight);
    remarkLabel->setWordWrap(true);
    remarkLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    remarkLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    mainLayout->addWidget(label);
    mainLayout->addWidget(remarkLabel, 1);
    mainLayout->addStretch();

    // mainLayout->addWidget(new AnimatedButton("测试一下", this));
    mainLayout->addWidget(reprintButton);
    mainLayout->addWidget(splitButton);
    mainLayout->addWidget(expiredButton);

    setLayout(mainLayout);

    loadDetail();
    applyStyles();
    setupConnections();
}

void RecordDetail::applyStyles() {
    setStyleSheet(
        R"(RecordDetail { background: #f0f2f5; }
        QLabel { font: 14px '微软雅黑'; color: #333; }
        QLabel[IsTitle='true'] {
            font: bold;
        }
        QPushButton {
            min-width: 60px;
            min-height: 48px;
            font: bold 18px;
            border: 1px solid #a0a0a0;
            border-radius: 4px;
            background: #ffffff;
        }
        QPushButton:disabled {
            background-color: #cccccc;
            color: #666666;
            border: 1px solid #999999;
        }
        QPushButton:hover {
            background: #e0e0e0;
        }
        QPushButton:pressed {
            background: #c0c0c0;
        }
        QPushButton[text="作废"] {
            background: #ff6666;
            color: white;
        }
        QPushButton[text="作废"]:hover {
            background: #dd4444;
            color: white;
        }
        QPushButton[text="作废"]:pressed {
            background: #aa3333;
            color: white;
        }
        )"
        );

    nameLabel->setStyleSheet("font: bold 18px '微软雅黑'; color: #1a73e8;");

    // titleLabel->setStyleSheet("font: bold 24px '微软雅黑'; color: #1a73e8;");
    // titleLabel->setAlignment(Qt::AlignCenter);
}

void RecordDetail::setupConnections() {
    auto dataProvider = DataProvider::instance();
    connect(dataProvider, &DataProvider::recordUpdate, this, &RecordDetail::recordUpdate);
    connect(reprintButton, &QPushButton::clicked, this, &RecordDetail::reprint);
    connect(splitButton, &QPushButton::clicked, this, &RecordDetail::split);
    connect(expiredButton, &QPushButton::clicked, this, &RecordDetail::expired);

}

void RecordDetail::reprint() {
    if (record == nullptr) {
        return;
    }
    DebounceHandler::instance()->triggerAction(reprintButton, [=]() {
        auto dataProvider = DataProvider::instance();
        dataProvider->PrintRecord(*record);
    });

}

void RecordDetail::split() {
    if (record == nullptr) {
        return;
    }
    DebounceHandler::instance()->triggerAction(splitButton, [=]() {
        auto dataProvider = DataProvider::instance();
        dataProvider->SplitRecord(record);
    });
}

void RecordDetail::expired() {
    if (record == nullptr) {
        return;
    }
    auto dataProvider = DataProvider::instance();
    dataProvider->DeleteRecord(record->LocalId);
}

void RecordDetail::recordUpdate(Record* r){
    record = r;
    loadDetail();
}

void RecordDetail::loadDetail(){
    if (record == nullptr) {
        nameLabel->setText("未选中");
        shortNameLabel->setText("未选中");
        makeAtLabel->setText("未选中");
        vailAtLabel->setText("未选中");
        remarkLabel->setText("未选中");
        saveTypeLabel->setText("未选中");
        standradLabel->setText("未选中");
        hasPartLabel->setText("未选中");
        statusLabel->setText("未选中");
        reprintButton->setDisabled(true);
        splitButton->setDisabled(true);
        expiredButton->setDisabled(true);
    } else {
        nameLabel->setText(record->Name);
        shortNameLabel->setText(record->ShortName);
        makeAtLabel->setText(record->GetMakeTimeString());
        vailAtLabel->setText(record->GetValidityTimeString());
        statusLabel->setText(record->GetStatusString());

        remarkLabel->setText(record->Remark);
        saveTypeLabel->setText(record->GetSaveTypeString());
        hasPartLabel->setText(record->HasPart == 2 ? "是" : "否");
        standradLabel->setText(
            QString("%1, %2").arg(GetOperationTypeString(record->OperationType))
                            .arg(GetValidityCountString(record->DateUnit, record->DateCount)));

        expiredButton->setDisabled(false);

        if (record->isExpired()) {
            nameLabel->setStyleSheet("font: bold 18px '微软雅黑'; color: #c92c2c");
            statusLabel->setStyleSheet("color: #c92c2c");
            vailAtLabel->setStyleSheet("color: #c92c2c");
            reprintButton->setDisabled(true);
            splitButton->setDisabled(true);
        } else {
            if (record->isExpiring()) {
                statusLabel->setStyleSheet("color: #c92c2c");
            } else {
                statusLabel->setStyleSheet("color: #333");
            }
            reprintButton->setDisabled(false);
            splitButton->setEnabled(record->HasPart == 2);
            nameLabel->setStyleSheet("font: bold 18px '微软雅黑'; color: #1a73e8;");
            vailAtLabel->setStyleSheet("color: #333");
        }
    }
}
